﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Tools
{
    public class ProcessClass
    {
        #region 开启进程
        public static bool StartProcess(string processName, string processFolder)
        {
            try
            {
                Process p = new Process();
                string processPath = Path.Combine(processFolder, processName);
                if (File.Exists(processPath))
                {
                    p = Process.Start(processPath);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public static bool StartProcess(string processName, string processFolder, string[] args)
        {
            if (args.Length == 0) return StartProcess(processName, processFolder);
            try
            {
                string processPath = Path.Combine(processFolder, processName);
                if (File.Exists(processPath))
                {
                    ProcessStartInfo processInfo = new ProcessStartInfo();
                    processInfo.FileName = processPath;
                    processInfo.CreateNoWindow = false;
                    processInfo.Arguments = null;
                    for(int i = 0; i < args.Length; i++)//添加参数
                    {
                        processInfo.Arguments += args[i] + " ";
                    }
                    Process.Start(processInfo);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region 结束进程
        public static bool KillProcess(string processName)
        {
            try
            {
                Process[] pro = Process.GetProcesses();//获取已开启的所有进程
                //遍历所有查找到的进程
                for (int i = 0; i < pro.Length; i++)
                {
                    //判断此进程是否是要查找的进程
                    if (pro[i].ProcessName.ToString().ToLower() == processName.ToLower())
                    {
                        pro[i].Kill();//结束进程
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
